﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DoAnHuongRung
{
    public partial class frmQuanLyNghiPhep : Form
    {
        public frmQuanLyNghiPhep()
        {
            InitializeComponent();
        }

        private void frmQuanLyNghiPhep_Load(object sender, EventArgs e)
        {
            LoadDataTxtTimkiem();
            LoadDtgNghiPhep();
            LoadCmbNhanVien();
            dateTimePicker1.MinDate = DateTime.Now;
        }

        private void LoadDataTxtTimkiem()
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            List<NhanVienDTO> list = NhanVienBUS.SelectAll();
            for (int i = 0; i < list.Count(); i++)
                auto.Add(list[i].HoTen);
            txtTimKiem.AutoCompleteCustomSource = auto;
        }

        private void txtTimKiemNCC_Click(object sender, EventArgs e)
        {
            if (txtTimKiem.Text.Equals("Tìm kiếm lịch làm việc của nhân viên"))
            {
                txtTimKiem.Text = "";
                txtTimKiem.ForeColor = Color.Black;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keydata)
        {
            if (keydata == Keys.Enter)
            {
                if (txtTimKiem.Focused == true && !txtTimKiem.Text.Equals(""))
                {
                    dtgLichLamViec.AutoGenerateColumns = false;
                    dtgLichLamViec.DataSource = LichLamViecBUS.Select_by_TenNhanVien(txtTimKiem.Text);
                    for (int i = 0; i < dtgLichLamViec.RowCount; i++)
                    {
                        dtgLichLamViec.Rows[i].Cells[6].Value = "Nghỉ phép";
                    }
                }
            }
            return false;
        }

        private void dtgLichLamViec_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string str = ((System.Windows.Forms.DataGridView)(sender)).Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            if (str.Equals("Nghỉ phép"))
            {
                frmUpdateNghiPhep frm = new frmUpdateNghiPhep(this);
                frm.ShowDialog();
            }
        }

        private void LoadDtgNghiPhep()
        {
            if (dateTimePicker1.Value != null)
            {
                dtgNghiPhep.AutoGenerateColumns = false;
                dtgNghiPhep.DataSource = NghiPhepBUS.SelectAll(dateTimePicker1.Value.ToString("MM/dd/yyyy"));
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            LoadDtgNghiPhep();
        }

        private void LoadCmbNhanVien()
        {
            cmbNhanVien.DisplayMember = "HoTen";
            cmbNhanVien.ValueMember = "MaNhanVien";
            cmbNhanVien.DataSource = NhanVienBUS.SelectAll();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Chắc chắn nghỉ hay không, vì sau khi nghỉ sẽ không xóa hoặc sửa được !!!", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    NghiPhepDTO np = new NghiPhepDTO();
                    np.MaNhanVien = int.Parse(cmbNhanVien.SelectedValue.ToString());
                    np.NgayBatDauNghi = dateTimePicker1.Value.ToString("MM/dd/yyyy");
                    np.SoNgayNghi = int.Parse(txtSoNgayNghi.Value.ToString());
                    List<LichLamViecDTO> listLLV = LichLamViecBUS.KiemTraNgayDoNhanVienCoLamHayKhong(np.NgayBatDauNghi, int.Parse(cmbNhanVien.SelectedValue.ToString()));
                    for (int i = 0; i < dtgLichLamViec.RowCount; i++)
                    {
                        dtgLichLamViec.Rows[i].Cells[6].Value = "Nghỉ phép";
                    }
                    if (listLLV.Count != 0)
                    {
                        dtgLichLamViec.AutoGenerateColumns = false;
                        dtgLichLamViec.DataSource = listLLV;
                    }
                    else
                    {
                        bool b = NghiPhepBUS.Insert(np);
                        if (b == true)
                            LoadDtgNghiPhep();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    bool b=NghiPhepBUS
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
        }
    }
}
